<?php 
namespace xfyzf\api\controller;

class WXMac_Protocol{
    public $api;
    public function __construct($url = ''){
        $this->api = empty($url)?"http://pc.wfkc.tech:999":$url;
    }
    
	public  function wxlogin(){//创建微信
    	$Header =  array('Content-Type: application/json-patch+json');
    	  $url= $this->api."/api/Client/WXCreate";
    	  
    	      $data=array(
    	          "Terminal"=>3,
                  "WxData"=> "data",
                  "Brand"=> "mac",
                  "Name"=> "mac-tegic",
                  "Imei"=> "string",
                  "Mac"=> "mac"
    	          );
    	      $res=xfhttp_post_yun($url,$Header,$data);
    	      $res=json_decode($res,true);
    	      
    	      if($res['data']['Guid']){
    	          $guid = $res['data']['Guid'];
    	          $qrcode=$this->api."/api/Login/WXGetLoginQrcode";
    	          $Header =  array('Content-Type: application/json-patch+json');
    	          $data = array(
                      'Guid'=>$guid
                      );
                
                 $res=json_decode(xfhttp_post_yun($qrcode,$Header,$data),true);
                 if($res['data']['qrcode']){
                     $result = ["uuid" => $res["data"]["uuid"], "guid" => strval($guid), "qr_url" =>"data:image/jpg;base64,". $res["data"]["qrcode"]];
                 }else{
                     return false;
                 }
    	      }else{
    	          return false;
    	      }
    	     return $result;
	}
	public  function wxCheckLoginQrcode($guid, $uuid){//二维码状态
	    $Header =  array('Content-Type: application/json-patch+json');
	    $WXCheck = $this->api."/api/Login/WXCheckLoginQrcode";
	    $post = array(
    	    'Guid'=>$guid,
    	    'Uuid'=>$uuid
    	    );
    	$res = json_decode(xfhttp_post_yun($WXCheck, $Header,$post), true);
	    if ($res["data"]["state"] == 0) {
    		$result = ["code" => 0, "msg" => "等待扫码中", "state" => $res["data"]["state"]];
    	} elseif ($res["data"]["state"] == 1) {
    		$result = ["code" => 2, "msg" => "等待确认中", "state" => $res["data"]["state"]];
    	} elseif ($res["data"]["state"] == 4) {
    		$result = ["code" => -1, "msg" => "系统错误,请联系管理员", "state" => $res["data"]["state"]];
    	} elseif ($res["data"]["state"] == 2) {
    		$WXLogin = $this->api. "/api/Login/WXSecLoginManual";
    		$pass = $res["data"]["wxnewpass"];
    		$wxid = $res["data"]["wxid"];
    		$post = array(
    		    "Guid"=>$guid,
    		    "Channel"=>1,
    		    "UserName"=>$wxid,
    		    "Password"=>$pass
    		    );
    		$res2 = json_decode(xfhttp_post_yun($WXLogin,$Header,$post), true);
    		$result = ["code" => 200, "msg" => "登录成功", "guid" => $guid];
    	}
    	return $result;
	}
	public  function WXHeartBeat($guid){
	    $Header =  array('Content-Type: application/json-patch+json');
	    $api= $this->api."/api/Common/WXHeartBeat";
	    $post = array(
    	    'Guid'=>$guid
    	  );
    	 $res = json_decode(xfhttp_post_yun($api, $Header,$post), true); 
    	 if($res['data']){
    	 	   $result = ["code" => 1, "msg" => "心跳正常"];
	 	}else{
	 	     $result = ["code" => -1, "msg" => "账户已经离线"];
	 	}
	 	return $result;
	}
	public  function WXF2FQrcode($guid,$fee,$Description){
	    $Header =  array('Content-Type: application/json-patch+json');
	    $api = $this->api."/api/Payment/WXTransferSetF2FFee";
	    $post = array(
	        'Fee'=>$fee*100,
	        'Description'=>$Description,
    	    'Guid'=>$guid
    	    );
    	$res = json_decode(xfhttp_post_yun($api, $Header,$post), true);    
    	$arrs = self::stripslashes_deep($res["data"]["reqText"]["buffer"]);
    	return $arrs;
	}
	public  function stripslashes_deep ( $value ){
        $value = is_array ( $value ) ?
        array_map ( 'stripslashes_deep' , $value ) :
        stripslashes ( $value );
        return $value ;
    }
    public  function Order($guid){
         $Header =  array('Content-Type: application/json-patch+json');
         $api=$this->api."/api/Message/WXSyncMsg";
         $post = array("Guid"=>$guid);
         $res = xfhttp_post_yun($api, $Header,$post); 
         $result=self::stripslashes_deep($res);
         return $result;
    }
    public  function Xwcode($guid, $appid = "wx264e9b6d4d484f51"){//获取 收款单sid
        $WXJSlogin = $this->api . "/api/Common/WXJSLogin";
        $Header =  array('Content-Type: application/json-patch+json');
        $post = array("Guid"=>$guid,"AppId"=>$appid);
        $res = json_decode(xfhttp_post_yun($WXJSlogin, $Header ,$post),true);
        $code=$res['data']['code'];
        $url = "https://payapp.wechatpay.cn/receiptwxmgr/account/list?miniprogram_version=3.11.12&js_code=".$code;
        $res=json_decode(get_curl($url),true);
        $sid=$res['sid'];
        $filteredData = [];
        // 使用正则表达式筛选特定格式的 account_id
        $pattern = '/^[a-zA-Z0-9]{28}$/'; // 匹配由28个字母或数字组成的字符串
        foreach ($res['data']['account_list'] as $account) {
            if (preg_match($pattern, $account['account_id'])){
                $filteredData['account_id'] = $account['account_id'];
                break;
            }
        }
        if($filteredData['account_id']){
            return ['code'=>1,'sid'=>$sid,'account_id'=>$filteredData['account_id']];
        }else{
            return ['code'=>-1];
        }
    }
    public  function WXID($sid,$fee,$remark,$account_id){
        $fee = $fee*100;
        $url = 'https://payapp.wechatpay.cn/receiptwxmgr/receipt/create?miniprogram_version=3.11.12&fee='.$fee.'&remark='.$remark.'&remark_pic_urls=&option_list=%5B%5D&account_id='.$account_id.'&account_type=2&sid='.$sid;
       
         $res=json_decode(get_curl($url),true);
         if($res['data']['receipt']['receipt_id']){
              return ['code'=>1,'receipt_id'=>$res['data']['receipt']['receipt_id']];
         }else{
             return ['code'=>-1,'msg'=>$res['msg']];
         }
    }
    public  function WXskdqrcode($sid,$account_id,$receipt_id){
        $url = 'https://payapp.wechatpay.cn/receiptwxmgr/receipt/getwxacode?miniprogram_version=3.11.12&wxacode_path_type=1&receipt_id='.$receipt_id.'&account_id='.$account_id.'&account_type=2&sid='.$sid;
        $res=json_decode(get_curl($url),true);
        if($res['data']['qrcode']){
            $result=array("code"=>1,"msg"=>"获取成功","qrcode"=>$res['data']['qrcode']);
        }else{
            $result=array("code"=>-1,"msg"=>"获取失败");
        }
        return $result;
    }
    
}


?>